# -*- coding: utf-8 -*-

from django.http import JsonResponse
from django.db.models import Q

from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination

from product.models import Product, Chipset
from product.serializers import ProductSerializer, ChipsetSerializer



def list_product_item(request):
    user = request.GET.get('user')
    if not user or user == 'guest':
        queryset = Product.objects.filter(Q(guest="True")).order_by('date')
        serializer = ProductSerializer(queryset, many=True)
    else:
        queryset = Product.objects.filter(Q(display="True")).order_by('name')
        serializer = ProductSerializer(queryset, many=True)
    return JsonResponse({"count":queryset.count(), "results":serializer.data}, safe=False)


def list_product_item_index(request):
    user = request.GET.get('user')
    if not user or user == 'guest':
        queryset = Product.objects.filter(Q(recommend="True") & Q(guest="True")).order_by('date')
        serializer = ProductSerializer(queryset, many=True)
    else:
        queryset = Product.objects.filter(Q(recommend="True") & Q(display="True")).order_by('name')
        serializer = ProductSerializer(queryset, many=True)
    return JsonResponse({"count":queryset.count(), "results":serializer.data}, safe=False)
 
 
def show_product_num(request):
    user = request.GET.get('user')
    if not user or user == 'guest':
        num = Product.objects.filter(Q(guest="True")).count()
    else:
        num = Product.objects.filter(Q(display="True")).count()
    return JsonResponse({'num_proj': num}, safe=False)


def list_chipset(request):
    user = request.GET.get('user')
    if not user or user == 'guest':
        queryset = Chipset.objects.filter(Q(guest="True")).order_by('name')
        serializer = ChipsetSerializer(queryset, many=True)
    else:
        queryset = Chipset.objects.filter(Q(display="True")).order_by('name')
        serializer = ChipsetSerializer(queryset, many=True)
    return JsonResponse({"count":queryset.count(), "results":serializer.data}, safe=False)



